package com.bsu.presentation.action;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RedirectingActionForward;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.actions.ForwardAction;

import com.bsu.bean.Blog;
import com.bsu.bean.User;
import com.bsu.database.dao.BlogDAO;
import com.bsu.presentation.form.BlogForm;
import com.bsu.presentation.form.UserForm;

public class BlogAction extends DispatchAction{
	private BlogDAO blogDao = new BlogDAO();
	private HttpSession session;
	
	public ActionForward myBlogs(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		BlogForm bf = (BlogForm) form;
		session = request.getSession();
		User user = new User();
		user = (User) session.getAttribute("user");
		ActionForward fg = null;
			int idUser = 0;
			idUser = user.getIdUser();
			
			try{
				bf.setBlogs(blogDao.getList(idUser));
				fg = mapping.findForward("my_blogs");
			}catch (Exception e) {
				fg = mapping.findForward("error");
			}
		
		return fg;
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		BlogForm bf = (BlogForm) form;
		Blog blog = new Blog();
		session = request.getSession();
		ActionForward fg;
		if(session.getAttribute("user") != null){
			bf.setBlogOne(blog);
			fg = mapping.findForward("create");
		}else{
			fg = mapping.findForward("error");
		}
		return fg;
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		BlogForm bf = (BlogForm) form;
		User user = new User();
		user = (User) session.getAttribute("user");		
		Blog blogOne = bf.getBlogOne();
		blogOne.setUser_iduser(user);
		ActionForward fg;
		try {
			if(blogOne.getIdBlog() > 0){
				blogDao.update(blogOne);
			}else{
				blogDao.add(blogOne);
			}	
				fg = myBlogs(mapping, form, request, response);

		} catch (Exception e) {
			bf = (BlogForm) form;
			return mapping.findForward("error");
		}

		
		return fg;
	}

	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		ActionForward fg;
		BlogForm bf = (BlogForm) form;
		Blog blog;
		int idBlog = 0;
		idBlog = bf.getId();
		
		try{
			blog = blogDao.get(idBlog);
			if(blog != null){
				bf.setBlogOne(blog);
				fg = mapping.findForward("view");
			}else{
				fg = mapping.findForward("error");
			}
			
			
		}catch (Exception e) {
			fg = mapping.findForward("error");
		}
		
		return fg;
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		ActionForward fg;
		BlogForm bf = (BlogForm) form;
		Blog blog;
		int idBlog = 0;
		idBlog = bf.getId();
		
		try{
			blog = blogDao.get(idBlog);
			if(blog != null){
				bf.setBlogOne(blog);
				fg = mapping.findForward("create");
			}else{
				fg = mapping.findForward("error");
			}
			
			
		}catch (Exception e) {
			fg = mapping.findForward("error");
		}
		
		return fg;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		BlogForm bf = (BlogForm) form;
		Blog blogOne = bf.getBlogOne();
		int id = blogOne.getIdBlog();
		ActionForward fg;
		try{
			if(id != 0)
				blogDao.delete(id);
				fg = myBlogs(mapping, form, request, response);

		}catch (Exception e) {
			fg = mapping.findForward("error");
		}
		
		return fg;
		
	}
	
	public ActionForward jsonParse(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		BlogForm bf = (BlogForm) form;
		Blog blogOne = bf.getBlogOne();
//		String query = request.getQueryString();
//
//		Map<String, String[]> map = request.getParameterMap();
//		    Iterator<Entry<String, String[]>> it = map.entrySet().iterator();
////		    w.write("POST values:<br>");
//		    while(it.hasNext()){
//		      Entry<String, String[]> e = it.next();
//		      if(!query.contains(e.getKey())){
//		             System.out.println(e.getKey());
//		      }
//		    }
		Enumeration parameterNames = request.getParameterNames();
//		JSON jsonObj = JSONObject.fromObject(request.getParameterMap());
//		System.out.println(jsonObj.toString());
		return null;
		
	}
}
